package com.beemans.common.ui.views

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.animation.TranslateAnimation
import android.widget.TextSwitcher
import android.widget.TextView
import android.widget.ViewSwitcher
import com.beemans.common.R
import java.util.*

/**
 * @author tiamosu
 * @date 2021/3/15
 *
 * 描述：文本信息轮播滚动
 */
class AutoScrollTextView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null
) : TextSwitcher(context, attrs), ViewSwitcher.ViewFactory {
    private var textSize = 12f
    private var textColor = Color.WHITE
    private var time = 0L
    private var currentId = -1
    private val textList = ArrayList<CharSequence>()

    private var startScrollCallback: (position: Int) -> Unit = {}
    private val startScrollRunnable by lazy {
        Runnable {
            startAutoScroll(startScrollCallback)
        }
    }

    init {
        context.obtainStyledAttributes(attrs, R.styleable.AutoScrollTextView).apply {
            textSize =
                getDimensionPixelSize(R.styleable.AutoScrollTextView_astv_textSize, 12).toFloat()
            textColor = getColor(R.styleable.AutoScrollTextView_astv_textColor, textColor)
            time = getInt(R.styleable.AutoScrollTextView_astv_duration, 1000).toLong()
            recycle()
        }

        setFactory(this)
    }

    fun setAnimTime(animIn: Long, animOut: Long) {
        if (textList.size > 1) {
            inAnimation = TranslateAnimation(0f, 0f, animIn.toFloat(), 0f).apply {
                duration = animIn
            }
            outAnimation = TranslateAnimation(0f, 0f, 0f, (-animOut).toFloat()).apply {
                duration = animOut
            }
        }
    }

    override fun setText(text: CharSequence) {
        (nextView as? TextView)?.text = text
        showNext()
    }

    /**
     * 设置数据源
     *
     * @param titles
     */
    fun setTextList(titles: MutableList<CharSequence>) {
        if (textList.isNotEmpty()) {
            textList.clear()
        }
        textList.addAll(titles)
        currentId = -1
    }

    /**
     * 开始滚动
     */
    fun startAutoScroll(callback: (position: Int) -> Unit = {}) {
        startScrollCallback = callback

        if (textList.size > 0) {
            currentId++
            val position = currentId % textList.size
            setText(textList[position])
            callback.invoke(position)
        }
        if (textList.size > 1) {
            stopAutoScroll()
            postDelayed(startScrollRunnable, time)
        }
    }

    /**
     * 停止滚动
     */
    fun stopAutoScroll() {
        removeCallbacks(startScrollRunnable)
    }

    override fun makeView(): View = TextView(context).apply {
        layoutParams = LayoutParams(-1, -1)
        gravity = Gravity.CENTER
        maxLines = 1
        setTextColor(textColor)
        setTextSize(TypedValue.COMPLEX_UNIT_PX, this@AutoScrollTextView.textSize)
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        stopAutoScroll()
    }
}